"""
This file contains the HTML tags represented as Objects. 

The TAG object is the base for the other objects.
"""

class Tag(object):
    """ base class for tags """
    attrs = []
    def __init__(self, parent=None, *args, **kwds):
        self.children = []
        self.parent = parent
        self.name = self.__class__.__name__.lower()

    def __repr__(self):
        return "<%s>" % self.__class__.__name__

    def find_child(self, name):
        search = []
        for tag in self.children:
            if tag.name == name.lower():
                return tag
        return None

    def __getattr__(self, attr):
        if attr in self.__dict__:
            return self.__dict__[attr]

        child = self.find_child(attr)
        if child: 
            return child
        raise AttributeError


class Html(Tag):
    """<html>"""

class Head(Tag):
    """<head>"""

class Title(Tag):
    """<title>"""

class Body(Tag):
    """<body>"""

class A(Tag):
    """<a>"""
    attrs = Tag.attrs + ['href']
    def __init__(self, parent=None, *args, **kwds):
        Tag.__init__(self, parent, *args, **kwds)
        self.href = ""

    def __getitem__(self, name):
        return self.attrs[name]

class Form(Tag):
    """<form>"""

def isclass(obj):
    return isinstance(obj, type)

all_tags = dict()
for name, value in globals().items():
    if isclass(value) and issubclass(value, Tag):
        all_tags[value.__name__.lower()] = value
